﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Movies
{
    public partial class FormBGProcesses : Form
    {
        private List<HeplerBGProcesses> list;
        public FormBGProcesses(List<HeplerBGProcesses> listPassed)
        {
            InitializeComponent();
            list = listPassed;
            timer.Enabled = true;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.Hide();
        }


        private GroupBox[] processName = null;
        private void BackgroundProcesses_Load(object sender, EventArgs e)
        {
            
            bgProcessPanel.Controls.Clear();
            if (list.Count > 0)
            {
                processName = new GroupBox[list.Count];
                int index = 0;
                foreach (HeplerBGProcesses helper in list)
                {
                    if (helper != null && !helper.Completed)
                    {
                        processName[index] = new GroupBox();
                        processName[index].BackColor = System.Drawing.SystemColors.Control;
                        processName[index].FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                        processName[index].Location = new System.Drawing.Point(3, 3);

                        processName[index].Size = new System.Drawing.Size(264, 43);
                        processName[index].TabIndex = 0;
                        processName[index].TabStop = false;
                        processName[index].Text = helper.Label;

                        ProgressBar progressBar = helper.ProgressBar;
                        //helper.ProgressBarOnWindow = progressBar;
                        progressBar.Location = new System.Drawing.Point(6, 14);
                        progressBar.Size = new System.Drawing.Size(252, 23);
                        progressBar.TabIndex = 0;
                        progressBar.Click += new System.EventHandler(this.progressBar_Click);
                        progressBar.Tag = helper;
                        helper.ParentControl = processName[index];
                        processName[index].Controls.Add(progressBar);
                        bgProcessPanel.Controls.Add(processName[index]);
                    }
                    index++;
                }
            }
             

        }

      //private int nameProvoder = 0;
        private void addProcessTab(HeplerBGProcesses helper, GroupBox processName)
        {

            processName.BackColor = System.Drawing.SystemColors.Control;
            processName.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            processName.Location = new System.Drawing.Point(3, 3);

            processName.Size = new System.Drawing.Size(264, 43);
            processName.TabIndex = 0;
            processName.TabStop = false;
            processName.Text = helper.Label;

            ProgressBar progressBar = helper.ProgressBar;
            progressBar.Location = new System.Drawing.Point(6, 14);

            progressBar.Size = new System.Drawing.Size(252, 23);
            progressBar.TabIndex = 0;
            progressBar.Click += new System.EventHandler(this.progressBar_Click);
            progressBar.Tag = helper;
            helper.ParentControl = processName;
            //processName.Controls.Add(progressBar);
            bgProcessPanel.Controls.Add(progressBar);

        }


        delegate void ShowHiddenForm(Form formHelper);

        private void showHiddenForm(Form formHelper)
        {
            if (formHelper.InvokeRequired)
            {
                ShowHiddenForm showHForm = new ShowHiddenForm(showHiddenForm);
                formHelper.Invoke(showHForm, new object[] { formHelper });
            }
            else
            {
                try
                {
                    formHelper.Show();
                }
                catch (Exception ex)
                {
                    try
                    {
                        formHelper.Dispose();
                    }
                    catch (Exception exe)
                    {
                        HelperContentProvider.OmitWarning(exe);
                    }
                    HelperContentProvider.OmitWarning(ex);
                    WinDisplayControllor.DisplayDialog("Unable to Show the Background Process",false);
                    
                }
            }
        }


        private void progressBar_Click(object sender, EventArgs e)
        {
            Form hiddenForm = null;
            Control progress = (Control)sender;
            if (progress.Tag != null)
            {
                HeplerBGProcesses helper = (HeplerBGProcesses)progress.Tag;
                hiddenForm = helper.Form;
                showHiddenForm(hiddenForm);
            }
        }

        

        private delegate void DisposeThis();
        private void disposeThis(){
            if (this.InvokeRequired)
            {
                DisposeThis dc = new DisposeThis(disposeThis);
                WinDisplayControllor.MDIParent.Invoke(dc, new object[] {  });
            }else{
                this.Dispose();
            }
        }


        private delegate void DisposeControl(HeplerBGProcesses ctl);
        private void disposeControl(HeplerBGProcesses helper){
            if (helper.ParentControl.InvokeRequired)
            {
                DisposeControl dc = new DisposeControl(disposeControl);
                this.Invoke(dc, new object[] { helper });
            }
            else
            {
                bgProcessPanel.Controls.Remove(helper.ParentControl);
            }
        }

        private void bgProcessPanel_ControlRemoved(object sender, ControlEventArgs e)
        {
            this.Dispose();
            HeplerBGProcesses.reloadBGProcessWin();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Interval = 1000;
            int total = 0;
            int count = 0;
            bool diposeThis = false;
            foreach (HeplerBGProcesses helper in list)
            {
                int progress = helper.updateProgress();
                if (progress == 100)
                {
                    diposeThis = true;
                    if (helper.ShowOnDone)
                    {
                        try
                        {
                            Form hiddenForm = helper.Form;
                            showHiddenForm(hiddenForm);
                        }
                        catch (Exception ex)
                        {
                            HelperContentProvider.OmitWarning(ex);
                        }
                    }
                    else
                    {
                        helper.Form.Dispose();
                    }
                }
                total += progress;
                count++;
            }
            if (count > 0)
                total = total / count;
            FormMainWindow mainWin = (FormMainWindow)WinDisplayControllor.MDIParent;
            mainWin.updateProcessInfo(total);

            if(diposeThis){
                this.Dispose();
                HeplerBGProcesses.reloadBGProcessWin();
                timer.Enabled = false;
            }
        }

        private void BackgroundProcesses_Shown(object sender, EventArgs e)
        {
        }

        private void BackgroundProcesses_FormClosing(object sender, FormClosingEventArgs e)
        {
        }

    }
}
